﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Xml;

namespace Common
{
   public static class ExtensionMethods
    {

        #region 值与字符操作
        
       /// <summary>
       /// 去掉字符串最后一个字符
       /// </summary>
       /// <param name="str"></param>
       /// <returns></returns>
       public static string CutLastChar(this String str)
       {
           if (str.Length > 0)
           {
               return str.Substring(0, str.Length-1);
           }
           else
           {
               return "";
           }
       }

       /// <summary>
       /// 转换成int32类型
       /// </summary>
       /// <param name="obj"></param>
       /// <returns></returns>
       public static int ToInt32(this Object obj)
       {
           try
           {
               if (obj == null)
               {
                   return 0;
               }
               else
               {
                   return Convert.ToInt32(obj);
               }
           }
           catch (Exception ex)
           {

               throw ex;
           }
       }

       /// <summary>
       /// 某列的值转换成int32类型
       /// </summary>
       /// <param name="obj"></param>
       /// <param name="data">列名</param>
       /// <returns>NULL则为0</returns>
       public static int ToInt32(this DataRow dr, string ColumnName)
       {
           try
           {
               if (dr == null)
               {
                   return 0;
               }
               else
               {
                   return Convert.ToInt32(dr[ColumnName]);
               }
           }
           catch (Exception ex)
           {

               throw ex;
           }
       }

       /// <summary>
       /// 转换成double类型
       /// </summary>
       /// <param name="obj"></param>
       /// <returns></returns>
       public static double ToDouble(this Object obj)
       {

           try
           {
               if (obj == null)
               {
                   return 0;
               }
               else
               {
                   return Convert.ToDouble(obj);
               }
           }
           catch (Exception ex)
           {

               throw ex;
           }
       }


       /// <summary>
       /// 某列的值转换成double类型
       /// </summary>
       /// <param name="obj"></param>
       /// <param name="data">列名</param>
       /// <returns>NULL则为0</returns>
       public static double ToDouble(this DataRow dr, string ColumnName)
       {

           try
           {
               if (dr == null)
               {
                   return 0;
               }
               else
               {
                   return Convert.ToDouble(dr[ColumnName]);
               }
           }
           catch (Exception ex)
           {

               throw ex;
           }
       }

       /// <summary>
       /// 四舍五入
       /// </summary>
       /// <param name="db"></param>
       /// <returns></returns>
       public static double Round(this double db)
       {
           if (db < 0)
           {
               db = Math.Round(db + 5 / Math.Pow(10, 3), 2, MidpointRounding.AwayFromZero);
           }
           else
           {
               db = Math.Round(Convert.ToDouble(db), 2, MidpointRounding.AwayFromZero);
           }

           return db;
       }
   
        #endregion


        #region 控件




        #endregion


        #region XML操作

       public static string FindValue(this XmlNodeList xnl, KeyValuePair<string,string> KnownProperty, string ReturnPropertyName)
       {
           foreach (XmlNode xn in xnl)
           {
               if (xn.Attributes[KnownProperty.Key].Value == KnownProperty.Value)
               {
                   if (ReturnPropertyName == "InnerText")
                   {
                       return xn.InnerText;
                   }
                   else
                   {
                       return xn.Attributes[ReturnPropertyName].Value;
                   }
               }
           }
           return KnownProperty.Value;
       }

        #endregion
    }
}
